<template>
  <Dialog :title="t('doubleChoiceRecall.audit.title')" v-model="dialogVisible" width="1400">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item :label="t('student.fields.studentName')" prop="studentName">
          <el-input
            v-model="queryParams.studentName"
            :placeholder="t('common.inputTextPlaceholder') + t('student.fields.studentName')"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item :label="t('student.fields.phone')" prop="studentMobile">
          <el-input
            v-model="queryParams.studentMobile"
            :placeholder="t('common.inputTextPlaceholder') + t('student.fields.phone')"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery">
            <Icon icon="ep:search" class="mr-5px" />
            {{ t('common.search') }}
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" class="mr-5px" />
            {{ t('common.reset') }}
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <vxe-table
        id="iDoubleChoiceRecallAudit-table"
        v-loading="loading"
        :data="list"
        stripe
        :custom-config="vxeTableConfig"
        :column-config="{ resizable: true }"
        :size="vxeTableSize"
      >
        <vxe-column
          :title="t('common.index')"
          type="seq"
          field="seq"
          width="60"
          align="center"
          fixed="left"
        />
        <vxe-column
          :title="t('student.fields.studentName')"
          field="studentName"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
          fixed="left"
        />
        <vxe-column
          :title="t('student.fields.recruitType')"
          field="recruitType"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.phone')"
          field="phone"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.batchId')"
          field="batchId"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.chioceStatus')"
          field="chioceStatus"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.chioceMentorId')"
          field="mentorName"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.interviewScore')"
          field="interviewScore"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        confirmJudges
        <vxe-column
          :title="t('student.fields.confirmJudges')"
          field="confirmJudges"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.birthdate')"
          field="birthdate"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
          :formatter="vxeDateFormatter2"
        />
        <vxe-column
          :title="t('student.fields.gender')"
          field="gender"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.politicalStatus')"
          field="politicalStatus"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.ethnic')"
          field="ethnic"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.identityNumber')"
          field="identityNumber"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.contactAddress')"
          field="contactAddress"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.email')"
          field="email"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.university')"
          field="university"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.is985')"
          field="is985"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
          :formatter="vxeBooleanFormatter"
        />
        <vxe-column
          :title="t('student.fields.is211')"
          field="is211"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
          :formatter="vxeBooleanFormatter"
        />
        <vxe-column
          :title="t('student.fields.isFirstClass')"
          field="isFirstClass"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
          :formatter="vxeBooleanFormatter"
        />
        <vxe-column
          :title="t('student.fields.studyMajor')"
          field="studyMajor"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.applyMajor')"
          field="applyMajor"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.majorRank')"
          field="majorRank"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.majorTotal')"
          field="majorTotal"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.classRank')"
          field="classRank"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.classTotal')"
          field="classTotal"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.cet4Score')"
          field="cet4Score"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.cet6Score')"
          field="cet6Score"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('student.fields.attachment')"
          field="attachment"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        >
          <template #default="scope">
            <FileObjectDownload :file-json="scope.row.attachment" />
          </template>
        </vxe-column>
        <vxe-column
          :title="t('doubleChoiceRecall.fields.reason')"
          field="reason"
          align="center"
          show-header-overflow
          show-overflow
          min-width="200"
          fixed="right"
        />
        <vxe-column
          :title="t('doubleChoiceRecall.fields.recallStatus')"
          field="recallStatus"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
          :visible="false"
        />
        <vxe-column
          :title="t('doubleChoiceRecall.fields.recallTime')"
          field="recallTime"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
          :formatter="vxeDateFormatter"
          :visible="false"
        />
        <vxe-column
          :title="t('common.operate')"
          field="operator"
          align="center"
          fixed="right"
          width="120"
        >
          <template #default="scope">
            <template v-if="scope.row.recallStatus === '待审核'">
              <el-button link type="success" @click="handleAudit(scope.row, true)">
                {{ t('doubleChoiceRecall.audit.pass') }}
              </el-button>
              <el-button link type="danger" @click="handleAudit(scope.row, false)">
                {{ t('doubleChoiceRecall.audit.deny') }}
              </el-button>
            </template>
            <template v-else>
              {{ scope.row.recallStatus }}
            </template>
          </template>
        </vxe-column>
      </vxe-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </Dialog>
</template>

<script setup lang="ts">
import { vxeDateFormatter, vxeDateFormatter2 } from '@/utils/formatTime'
import {
  DoubleChoiceRecallApi,
  DoubleChoiceRecallListRespVO
} from '@/api/recruit/doublechoicerecall'
import type { VxeTablePropTypes } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import FileObjectDownload from '@/components/FileObjectDownload/index.vue'
import { vxeBooleanFormatter } from '@/utils/formatter'

/** 双选申请解除 列表 */
defineOptions({ name: 'DoubleChoiceRecallAudit' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false)

const loading = ref(true) // 列表的加载中
const list = ref<DoubleChoiceRecallListRespVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  batchId: undefined,
  recruitStudentId: undefined,
  studentName: undefined,
  studentMobile: undefined
})
const queryFormRef = ref() // 搜索的表单

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  queryParams.batchId = (currentBatchId.value ?? undefined) as any
  loading.value = true
  try {
    const data = await DoubleChoiceRecallApi.viewDoubleChoiceRecallPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const handleAudit = async (row: DoubleChoiceRecallListRespVO, pass: boolean) => {
  let data
  if (pass) {
    // 通过
    try {
      await message.confirm(t('doubleChoiceRecall.audit.passTip'))
    } catch {
      return
    }
    data = {
      id: row.id,
      recallStatus: '已通过',
      choiceId: row.choiceId
    }
  } else {
    // 拒绝
    try {
      await message.confirm(t('doubleChoiceRecall.audit.denyTip'))
    } catch {
      return
    }
    data = {
      id: row.id,
      recallStatus: '已拒绝',
      choiceId: row.choiceId
    }
  }

  try {
    loading.value = true
    await DoubleChoiceRecallApi.auditDoubleChoiceRecall(data)
    message.success(t('common.operSuccess'))
  } finally {
    loading.value = false
  }
  await getList()
}

const currentBatchId = ref<string>()
const open = (batchId?: string) => {
  dialogVisible.value = true
  currentBatchId.value = batchId
  handleQuery()
}

defineExpose({
  open
})
</script>
